package cn.my.framework.common.controller;

import cn.my.framework.common.entity.User;
import cn.my.framework.common.service.UserPermService;
import cn.my.framework.util.ConstantData;
import com.github.pagehelper.Page;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangl_lc on 2017/7/6.
 */
@Controller
@RequestMapping("user")
public class UserPermController {

    Logger logger = LoggerFactory.getLogger(UserPermController.class);

    @Autowired
    UserPermService permService;

    @RequestMapping(value = "/list",method = {RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody Map<String,Serializable> listUsers(HttpServletRequest req){
        logger.info("...... list users");
        String curr = req.getParameter("curr");
        String limit = req.getParameter("limit");
        int pageSize = StringUtils.isNoneBlank(limit) ? Integer.parseInt(limit) : ConstantData.pageSize;
        curr = Strings.isNullOrEmpty(curr) ? "1":curr;
        logger.info("...... list users ,curr={}",curr);
        Map<String,Serializable> map = new HashMap<>();
        Page<User> userPage = permService.selectUsers(Integer.valueOf(curr),pageSize);
        map.put("result",new ArrayList<>(userPage.getResult()));
        map.put("pages",userPage.getPages());
        map.put("count",userPage.getTotal());
        map.put("limit",userPage.getPageSize());
        return map;
    }

    @RequestMapping(value = "/list3",method = {RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody Map<String,Serializable> listUsers3(){
        logger.info("...... list users3");
        Map<String,Serializable> map = new HashMap<>();
        Page<User> userPage = permService.selectUsers(1,2);
        map.put("result",new ArrayList<>(userPage.getResult()));
        map.put("pages",userPage.getPages());
        return map;
    }

    @RequestMapping(value = "/list2",method = {RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody List<Map<String,String>> doList(){
        List list = Lists.newArrayList();
        Map<String,String> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        list.add(map);
        map = new HashMap<>();
        map.put("key1","value12");
        map.put("key2","value22");
        list.add(map);
        return list;
    }

}
